package dao

import (
	"advertising/internal/dal"
	"advertising/internal/models"
	"context"
	"path"
	"strings"
	"time"

	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

type Material struct{}

func (Material) Create(ctx context.Context, db *gorm.DB, name string, Type int8, folderID string, position int8, Duration int64, filePath, Size string, fileSize int64, md5 string, merchantID string, merchantName string, userID string, username string) (id string, err error) {
	m := dal.Use(db).Material

	var data models.Material
	data.ID, _ = helper.UUID{}.String()
	data.Name = name
	data.Type = Type
	data.FolderID = folderID
	data.Position = position
	data.Duration = Duration
	data.Suffix = path.Ext(filePath)
	data.Size = Size
	data.FileSize = fileSize
	data.Status = 1 // 等待审核
	data.FilePath = filePath
	data.CheckMd5 = md5
	data.MerchantID = merchantID
	data.MerchantName = merchantName
	data.CreateUser = userID
	data.CreateUserName = username
	data.CreatedAt = time.Now().Unix()
	m.WithContext(ctx).Create(&data)
	return data.ID, nil
}

// 同名是否存在
func (Material) SameNameExist(ctx context.Context, db *gorm.DB, merchantID, name, ExceptID string) (count int64) {
	m := dal.Use(db).Material
	tx := m.WithContext(ctx).
		Where(m.DeletedAt.Eq(0))
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if name != "" {
		tx = tx.Where(m.Name.Eq(strings.TrimSpace(name)))
	}
	if ExceptID != "" {
		tx = tx.Where(m.ID.Neq(ExceptID))
	}
	count, _ = tx.Count()
	return
}

// 获取详情
func (Material) Detail(ctx context.Context, db *gorm.DB, merchantID, ID string, folderID string, Status int8) (data *models.Material, count int64) {
	m := dal.Use(db).Material
	tx := m.WithContext(ctx).
		Where(m.DeletedAt.Eq(0))
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if ID != "" {
		tx = tx.Where(m.ID.Eq(ID))
	}
	if folderID != "" {
		if strings.Contains(folderID, ",") {
			tx = tx.Where(m.FolderID.In(strings.Split(folderID, ",")...))
		} else {
			tx = tx.Where(m.FolderID.Eq(folderID))
		}
	}
	if Status != 0 {
		tx = tx.Where(m.Status.Eq(Status))
	}
	count, _ = tx.Count()
	info, _ := tx.Take()
	return info, count
}

type MaterialIDNameList struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

// 获取素材名称列表
func (Material) GetMaterialNameList(ctx context.Context, db *gorm.DB, id string) (res []MaterialIDNameList, err error) {
	m := dal.Use(db).Material
	tx := m.WithContext(ctx).
		Where(m.DeletedAt.Eq(0))
	if id != "" {
		if strings.Contains(id, ",") {
			tx = tx.Where(m.ID.In(strings.Split(id, ",")...))
		} else {
			tx = tx.Where(m.ID.Eq(id))
		}
	}

	tx.Order(m.ID.Desc()).Select(m.ID, m.Name).Scan(&res)
	return res, nil
}

// 获取列表
func (Material) List(ctx context.Context, db *gorm.DB, merchantID, id, name string, Type, Status []int8, createUserName string, position, page, limit int, folderID string) (data []*models.Material, count int64, err error) {
	m := dal.Use(db).Material
	tx := m.WithContext(ctx).
		Where(m.DeletedAt.Eq(0))
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if id != "" {
		if strings.Contains(id, ",") {
			tx = tx.Where(m.ID.In(strings.Split(id, ",")...))
		} else {
			tx = tx.Where(m.ID.Eq(id))
		}
	}
	if name != "" {
		tx = tx.Where(m.Name.Like("%" + strings.TrimSpace(name) + "%"))
	}
	if len(Type) > 0 {
		tx = tx.Where(m.Type.In(Type...))
	}
	if len(Status) > 0 {
		tx = tx.Where(m.Status.In(Status...))
	}
	if len(Type) == 1 && Type[0] == 1 {
		if position != 0 { // 图片素材可筛选位置
			tx = tx.Where(m.Position.Eq(int8(position)))
		}
	}
	if createUserName != "" {
		tx = tx.Where(m.CreateUserName.Like("%" + strings.TrimSpace(createUserName) + "%"))
	}
	if folderID != "" {
		tx = tx.Where(m.FolderID.Eq(folderID))
	}

	MaterialList, count, _ := tx.Order(m.ID.Desc()).FindByPage(Offset(page, limit))
	return MaterialList, count, nil
}

// 素材删除
func (Material) MaterialDelete(ctx context.Context, db *gorm.DB, merchantID, ID string) (err error) {
	m := dal.Use(db).Material
	tx := m.WithContext(ctx).
		Where(m.DeletedAt.Eq(0))
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if ID != "" {
		tx = tx.Where(m.ID.Eq(ID))
	}
	tx.UpdateColumn(m.DeletedAt, time.Now().Unix())
	return nil
}

// 素材修改文件夹
func (Material) MaterialUpdateFolder(ctx context.Context, db *gorm.DB, merchantID, ID, folderID string) (err error) {
	m := dal.Use(db).Material
	tx := m.WithContext(ctx).
		Where(m.DeletedAt.Eq(0))
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if ID != "" {
		tx = tx.Where(m.ID.In(strings.Split(ID, ",")...))
	}
	tx.UpdateColumn(m.FolderID, folderID)
	return nil
}

// 素材修改状态
func (Material) ChangeStatus(ctx context.Context, db *gorm.DB, merchantID, ID string, currentStatus []int8, status int8) (err error) {
	m := dal.Use(db).Material
	tx := m.WithContext(ctx).
		Where(m.DeletedAt.Eq(0))
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if ID != "" {
		if strings.Contains(ID, ",") {
			tx = tx.Where(m.ID.In(strings.Split(ID, ",")...))
		} else {
			tx = tx.Where(m.ID.Eq(ID))
		}
	}
	if len(currentStatus) > 0 {
		tx = tx.Where(m.Status.In(currentStatus...))
	}
	tx.UpdateColumn(m.Status, status)
	return nil
}
